package com.sojson.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.controller.param.verify.CouponGoodsParamVerify;
import com.sojson.project.entity.po.CouponGoods;
import com.sojson.project.service.CouponGoodsService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 券可用商品控制器
 * 
 * @author liu
 * @date 2021-06-02
 */
@Api(tags = "券可用商品管理")
@RestController
@RequestMapping("/couponGoods")
public class CouponGoodsController {

    @Autowired
    private CouponGoodsService couponGoodsService;

    /**
     * 单条插入
     * 
     * @param couponGoods
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/couponGoods/insert')")
    @ApiOperation("给券可用商品添加一条数据")
    public Result<Integer> insert(@RequestBody CouponGoods couponGoods) {
        // 校验参数
        Result<Integer> paramResult = CouponGoodsParamVerify.insert(couponGoods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        couponGoods.insertInit();
        return Result.success(couponGoodsService.insert(couponGoods));
    }

    /**
     * 批量插入
     * 
     * @param couponGoods
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/couponGoods/insertList')")
    @ApiOperation("给券可用商品添加多条数据")
    public Result<Integer> insertList(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.insertList(couponGoods));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param couponGoods
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/couponGoods/deleteByDto')")
    @ApiOperation("通过对象批量删除券可用商品数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.deleteByDto(couponGoods));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/couponGoods/countAll')")
    @ApiOperation("统计券可用商品全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(couponGoodsService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/couponGoods/listAll')")
    @ApiOperation("查询券可用商品所有数据(包含逻辑删除的)")
    public Result<List<CouponGoods>> listAll() {
        return Result.success(couponGoodsService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param couponGoods
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/couponGoods/countByDto')")
    @ApiOperation("根据对象统计券可用商品数据")
    public Result<Integer> countByDto(CouponGoods couponGoods) {
        return Result.success(couponGoodsService.countByDto(couponGoods));
    }

    /**
     * 根据对象查询
     * 
     * @param couponGoods
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/couponGoods/listByDto')")
    @ApiOperation("根据对象查询券可用商品数据")
    public Result<List<CouponGoods>> listByDto(CouponGoods couponGoods) {
        return Result.success(couponGoodsService.listByDto(couponGoods));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/couponGoods/deleteById/{id}')")
    @ApiOperation("通过id删除券可用商品数据(物理删除)")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(couponGoodsService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/couponGoods/deleteByIds/{ids}')")
    @ApiOperation("通过id数组删除券可用商品数据(物理删除)")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(couponGoodsService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param couponGoods
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/couponGoods/updateAllById')")
    @ApiOperation("通过id更新券可用商品数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.updateAllById(couponGoods));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param couponGoods
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/couponGoods/updateAllByIdList')")
    @ApiOperation("通过id列表更新券可用商品数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.updateAllByIdList(couponGoods));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param couponGoods
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/couponGoods/updateDtoListAllById')")
    @ApiOperation("通过id列表更新券可用商品数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.updateDtoListAllById(couponGoods));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param couponGoods
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/couponGoods/updateById')")
    @ApiOperation("通过id更新券可用商品数据(局部更新)")
    public Result<Integer> updateById(@RequestBody CouponGoods couponGoods) {
        // 校验参数
        Result<Integer> paramResult = CouponGoodsParamVerify.updateById(couponGoods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        couponGoods.updateInit();
        couponGoods.setUpdateNull();
        return Result.success(couponGoodsService.updateById(couponGoods));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param couponGoods
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/couponGoods/updateByIdList')")
    @ApiOperation("通过id列表更新券可用商品数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.updateByIdList(couponGoods));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param couponGoods
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/couponGoods/updateDtoListById')")
    @ApiOperation("通过id列表更新券可用商品数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody CouponGoods couponGoods) {
        return Result.success(couponGoodsService.updateDtoListById(couponGoods));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/couponGoods/findById/{id}')")
    @ApiOperation("通过id查询券可用商品数据")
    public Result<CouponGoods> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(couponGoodsService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/couponGoods/findByIds/{ids}')")
    @ApiOperation("通过id数组查询券可用商品数据")
    public Result<List<CouponGoods>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(couponGoodsService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param couponGoods
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/couponGoods/findIdByDto')")
    @ApiOperation("通过对象查询券可用商品id列表")
    public Result<List<Integer>> findIdByDto(CouponGoods couponGoods) {
        return Result.success(couponGoodsService.findIdByDto(couponGoods));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/couponGoods/clear')")
    @ApiOperation("清空券可用商品数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(couponGoodsService.clear());
    }

    /**
     * 根据对象查询
     * 
     * @param couponGood
     * @return
     */
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('/couponGoods/list')")
    public Object list(CouponGoods couponGoods) {
        List<CouponGoods> list = couponGoodsService.list(couponGoods);
        return Result.success(list);
    }

}